package com.example.hospital_208.controller;

import com.example.hospital_208.po.Notice;
import com.example.hospital_208.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/notices")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    // 查询所有通知
    @GetMapping
    public Map<String, Object> findAll() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Notice> notices = noticeService.findAll();
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", notices);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    // 根据ID查询
    @GetMapping("/{id}")
    public Map<String, Object> findById(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Notice notice = noticeService.findById(id);
            if (notice != null) {
                result.put("code", 200);
                result.put("message", "查询成功");
                result.put("data", notice);
            } else {
                result.put("code", 404);
                result.put("message", "未找到该通知");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    // 根据日期查询
    @GetMapping("/search")
    public Map<String, Object> findByDay(@RequestParam String day) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Notice> notices = noticeService.findByDay(day);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", notices);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    // 新增通知
    @PostMapping
    public Map<String, Object> insert(@RequestBody Notice notice) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = noticeService.insert(notice);
            if (count > 0) {
                result.put("code", 201);
                result.put("message", "新增成功");
                result.put("data", notice.getId());
            } else {
                result.put("code", 500);
                result.put("message", "新增失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "新增失败：" + e.getMessage());
        }
        return result;
    }

    // 更新通知
    @PutMapping
    public Map<String, Object> update(@RequestBody Notice notice) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = noticeService.update(notice);
            if (count > 0) {
                result.put("code", 200);
                result.put("message", "更新成功");
            } else {
                result.put("code", 404);
                result.put("message", "未找到该通知");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    // 删除通知
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = noticeService.delete(id);
            if (count > 0) {
                result.put("code", 200);
                result.put("message", "删除成功");
            } else {
                result.put("code", 404);
                result.put("message", "未找到该通知");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }
}